package com.airsystem.pos.bengkel.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.ForeignKey;

/**
 * @author Fanny Irawan Sutawanir (fannyirawans@gmail.com)
 */

@Entity
@Table(name = "tbl_detail_layanan")
public class DetailLayanan implements Serializable {

	@Transient
	public static final String ALIAS = "detail_layanan";

	@Transient
	private static final String DISPLAY = "%s [Rp. %s]";

	@Transient
	private int choice;

	@Id
	@GeneratedValue
	private Long id;

	@ManyToOne
	@ForeignKey(name = "FK_LAYANAN")
	private Layanan layanan;

	@ManyToOne
	@JoinColumn(name = "detail_kendaraan", nullable = false)
	@ForeignKey(name = "FK_LAYANAN_KENDARAAN")
	private DetailKendaraan detail_kendaraan;

	@OneToMany(mappedBy = "detail_layanan", fetch = FetchType.LAZY)
	private Set<DetailOrderLayanan> detail_order_layanan = new HashSet<DetailOrderLayanan>(0);

	private BigDecimal harga;

	public String toString() {
		return String.format(DISPLAY, layanan.getNama(), harga.toBigInteger().intValue());
	}

	public int getChoice() {
		return choice;
	}

	public void setChoice(int choice) {
		this.choice = choice;
	}

	public String getSearchColumn() {
		StringBuffer sb = new StringBuffer();

		switch (choice) {
		case 1:
			return sb.append(Layanan.ALIAS).append(".nama").toString();
		case 2:
			return sb.append(DetailLayanan.ALIAS).append(".harga").toString();
		case 3:
			return sb.append(DetailKendaraan.ALIAS).append(".kendaraan.merk").toString();
		case 4:
			return sb.append(DetailKendaraan.ALIAS).append(".sub_merk").toString();

		default:
			return sb.append(Layanan.ALIAS).append(".nama").toString();
		}
	}

	public String getSearchKeyword() {
		switch (choice) {
		case 1:
			return getLayanan().getNama();
		case 2:
			return getHarga().toString();
		case 3:
			return getDetail_kendaraan().getKendaraan().getMerk();
		case 4:
			return getDetail_kendaraan().getSub_merk();

		default:
			return "";
		}
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Layanan getLayanan() {
		return layanan;
	}

	public void setLayanan(Layanan layanan) {
		this.layanan = layanan;
	}

	public DetailKendaraan getDetail_kendaraan() {
		return detail_kendaraan;
	}

	public void setDetail_kendaraan(DetailKendaraan detail_kendaraan) {
		this.detail_kendaraan = detail_kendaraan;
	}

	public Set<DetailOrderLayanan> getDetail_order_layanan() {
		return detail_order_layanan;
	}

	public void setDetail_order_layanan(Set<DetailOrderLayanan> detail_order_layanan) {
		this.detail_order_layanan = detail_order_layanan;
	}

	public String getHarga_s() {
		return (harga == null) ? "0" : String.valueOf(harga.intValue());
	}

	public BigDecimal getHarga() {
		return harga;
	}

	public void setHarga(BigDecimal harga) {
		this.harga = harga;
	}
}